package demo4.mapper;

import demo4.pojo.UserInfo;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserMapper implements IUserMapper{
    @Autowired
    private IDBUtil dbUtil;
    public int insert(UserInfo user){
         /*
            1:获取数据库连接
            2:创建Connection对象
            3:创建PreparedStatement对象
            4:执行SQL语句
            5:接收数据库处理结果
            6:关闭数据库链接
            7:返回结果
         */
        System.out.println("UserMapper:插入User表");
        try (Connection conn=dbUtil.getConnection()){
            String sql="INSERT INTO userinfo(username,password,nickname,age) VALUES(?,?,?,?)";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getNickname());
            ps.setInt(4,user.getAge());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public String login(String username,String password){
        try (Connection conn=dbUtil.getConnection()){
            /*
            SELECT id,username,password,nickname,age
            FROM userinfo
            WHERE username=? AND password=?
             */
            String sql="SELECT id,username,password,nickname,age\n" +
                    "            FROM userinfo\n" +
                    "            WHERE username=? AND password=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet res=ps.executeQuery();
            if(res.next()){
                return "登录成功";
            }else {
                return "登录失败";
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    public List<UserInfo> userlist(){
        try (Connection conn= dbUtil.getConnection();){
            /*
            SELECT id,username,password,nickname,age
            FROM userinfo
             */
            String sql="SELECT id,username,password,nickname,age\n" +
                    "            FROM userinfo";
            Statement state=conn.createStatement();
            ResultSet rs=state.executeQuery(sql);
            List<UserInfo> list=new ArrayList<>();
            while (rs.next()){
                UserInfo user=new UserInfo();
                user.setId(rs.getInt("id"));
                user.setAge(rs.getInt("age"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                list.add(user);
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

